﻿$(function () {
    var start = {
        elem: "#start",
        format: "YYYY-MM-DD",
        istime: false,
        istoday: false,
    };
    var end = {
        elem: "#end",
        format: "YYYY-MM-DD",
        istime: false,
        istoday: false,
    };
    $("#start").val(TimeObjectUtil.getMonthDateTime(0).startTime);
    $("#end").val(TimeObjectUtil.getMonthDateTime(0).endTime);
    laydate(start);
    laydate(end);
    $('#State').multiselect({  
        buttonClass: 'btn btn-link',//显示按钮style  
        buttonWidth: '150px',//按钮宽度  
        inheritClass: true,//继承原来select的button的class  
        //buttonContainer: '<div class="btn-group" />',//承载按钮和下拉框  
        //selectedClass: 'multiselect-selected',//选中项样式  
        //optionClass: function(element) {  
        //    var value = $(element).val();  
   
        //    if (value%2 == 0) {  
        //        return 'even';  
        //    }  
        //    else {  
        //        return 'odd';  
        //    }  
        //},  
        //optionLabel: function(element) {  
        //    return $(element).html() + '(' + $(element).val() + ')';  
        //},  
        //buttonWidth: '150px',//选中内容长度超过150显示4selected  
        //enableClickableOptGroups: true,//同时取组或者all  
        //enableCollapsibleOptGroups: true,//组可折叠  
        //enableFiltering: true,//过滤  
        //filterPlaceholder: 'Search for something...',  
        //filterBehavior: 'value',//根据value或者text过滤  
        //enableFullValueFiltering: true,//能否全字匹配  
        //enableCaseInsensitiveFiltering: true,//不区分大小写  
        //includeSelectAllOption: true,//全选  
        //selectAllText: 'Check all!',//全选的checkbox名称  
        selectAllNumber: false,//true显示allselect（6）,false显示allselect  
        //selectAllName: 'select-all-name',  
        //selectAllValue: 'select-all-value',//可以为strig或者数字  
        //selectAllJustVisible: false,//选择所有的。true为全选可见的  
        //onSelectAll: function() {  
        //    alert('onSelectAll triggered.');  
        //},  
        //disableIfEmpty: true,//没有选项时readonly  
        //disabledText: 'Disabled ...',//disabled时显示的文字说明  
        ////下拉选项摆放在右侧  
        //dropRight: true,  
        ////下拉选项摆放在顶部  
        //maxHeight: 400,  
        //dropUp: true,  
        ////摆放在左侧默认为200  
        //maxHeight: 200,  
        ////见服务器端名称  
        //checkboxName: 'multiselect[]',  
        ////初始化  
        //onInitialized: function(select, container) {  
        //    alert('Initialized.');  
        //},  
        //onChange: function (option, checked) {//change事件改变   
        //    console.log(option.length + ' options ' + (checked ? 'selected' : 'deselected'));  
        //},  
        ////下拉回调函数  
        //onDropdownShow: function (event) {  
        //    alert('Dropdown shown.');  
        //},  
        ////下拉框关闭回调函数  
        //onDropdownHide: function (event) {  
        //    alert('Dropdown closed.');  
        //},  
        ////点击select调用，然后显示出下拉框  
        //onDropdownHidden: function (event) {  
        //    alert('Dropdown closed.');  
        //},  
  
    });  
    initTable();
    $('#AddOutmoneyOrders').click(function () {
        var selected = $table.bootstrapTable('getSelections');
        if (selected.length == 0) {
            layer.msg("请选择您要操作的数据！", { icon: 5, time: 2000 });
            return;
        }

        var arr = [];
        for (var i = 0; i < selected.length; i++) {
            arr.push(selected[i].id);
        }

        var ids = arr.join(",");
        parent.layer.open({
            type: 2,
            title: '加入出款单',
            shadeClose: false,
            shade: 0.4,
            area: ['500px', '350px'],
            content: '/Account/OutMoneyOrders/AddOutMoneyOrdersRunnings?BrokerageRunningsIDs='+ids+'&ModuleID=' + $('#ModuleID').val(),//?depid=' + $('#depart_id').val(),//$.getUrlParam('id'),// 
            end: function () {
                refreshTable();
            }
        });
    });
    if (($('#BrokerageTypeID').val() == 3 && isAuthority('168')) || ($('#BrokerageTypeID').val() == 7 && isAuthority('205')) || ($('#BrokerageTypeID').val() == 10 && isAuthority('282'))) {
        $('#AddOutmoneyOrders').show();
    } else {
        $('#AddOutmoneyOrders').hide();
    }
    //$table.bootstrapTable('hideColumn', 'optionStr');

});
function selecttime(t) {
    switch (t) {
        case 1:
            $("#start").val(laydate.now(-2));
            $("#end").val(laydate.now(0));
            break;
        case 2:
            $("#start").val(laydate.now(-(new Date().getDay() == 0 ? 6 : new Date().getDay() - 1)));
            $("#end").val(laydate.now(6-(new Date().getDay() == 0 ? 6 : new Date().getDay() - 1)));
            break;
        case 3:
            $("#start").val(laydate.now(-(new Date().getDay() == 0 ? 6 : new Date().getDay() - 1)-7));
            $("#end").val(laydate.now(6 - (new Date().getDay() == 0 ? 6 : new Date().getDay() - 1)-7));
            break;
        case 4:
            $("#start").val(TimeObjectUtil.getMonthDateTime(0).startTime);
            $("#end").val(TimeObjectUtil.getMonthDateTime(0).endTime);
            break;
        case 5:
            $("#start").val(TimeObjectUtil.getMonthDateTime(-1).startTime);
            $("#end").val(TimeObjectUtil.getMonthDateTime(-1).endTime);
            break;
        case 6:
            $("#start").val(TimeObjectUtil.getMonthDateTime(-(new Date().getMonth())).startTime);
            $("#end").val(TimeObjectUtil.getMonthDateTime(12-(new Date().getMonth()+1)).endTime);
            break;
        case 7:
            $("#start").val(TimeObjectUtil.getMonthDateTime(-(new Date().getMonth()+12)).startTime);
            $("#end").val(TimeObjectUtil.getMonthDateTime(12 - (new Date().getMonth() + 13)).endTime);
            break;
        default:

    }
}

var $table = $('#table');
function initTable() {
    $table.bootstrapTable({
        url: '/Account/Brokerage/GetBrokerageList',            //数据来源地址
        method: 'post',                     //数据请求方式
        striped: true,                      //是否显示行间隔色
        cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
        pagination: true,                   //是否启用分页
        sidePagination: 'server',           //在服务器端分页
        queryParams: queryParams,           //传递参数
        pageNumber: 1,                      //初始化加载第一页，默认第一页
        pageSize: 20,                       //每页的记录行数（*）
        pageList: [10, 20, 30, 40, 50],     //可供选择的每页的行数（*）
        clickToSelect: true,                //是否启用点击选中行
        //toolbar: "#tableToolBar",
        height: getHeight(),
        search: true,
        showRefresh: true,
        showColumns: true,
        showExport: true,                     //是否显示导出
        exportDataType: "basic",              //basic', 'all', 'selected'.
        exportTypes: ['excel'],
        exportOptions:{fileName:'123'},
        toolbar: '#exampleToolbar',
        iconSize: 'outline',
        icons: {
            refresh: 'glyphicon-repeat',
            columns: 'glyphicon-list'
        },
        toolbar: '#exampleToolbar',
        columns: [{
            width: 38,
            field: 'aaaaaaa',
            checkbox: true,
        }, {
            title: '编号',
            align: 'center',
            field: 'id',
        }, {
            field: 'itemName',
            title: '项目名称',
            align: 'center',
        }, {
            field: 'customerName',
            title: '客户姓名',
            align: 'center',
        }, {
            field: 'customerPhone',
            title: '客户电话',
            align: 'center',
        }, {
            field: 'itemTag',
            title: '地址门牌号',
            align: 'center',
        }, {
            field: 'itemTime',
            title: '认购时间',
            align: 'center',
        }, {
            field: 'intermediaryName',
            title: '中介',
            align: 'center',
            formatter: function (value, row, index) {
                    return [
                    '<a class="sawpic" href="javascript:showIntermediaryInfo(' + row.intermediaryID + ')" title="查看中介信息">',
                    value,
                    '</a>  '
                    ].join('');
            },

        }, {
            field: 'userName',
            title: '经纪人',
            align: 'center',
        }, {
            field: 'itemImageType1Number',
            title: '带看单图片',
            align: 'center',
            formatter: function (value, row, index) {
                var groupid = 0;
                var imagetype = 1;
                switch ($('#ModuleID').val()) {
                    case '1':
                        groupid = 3;
                        imagetype = 1;
                        break;
                    case '2':
                    case '3':
                        groupid = 5;
                        imagetype = 1;
                        break;
                    case '4':
                        groupid = 7;
                        imagetype = 1;
                        break;
                    case '5':
                        groupid = 9;
                        imagetype = 1;
                        break;
                    default:

                }
                if (parseInt(row['itemImageType1Number']) == 0) {
                    return [
                    '<a class="sawpic" href="javascript:sawPic(' + groupid + ',' + imagetype + ',' + row.runningsID + ')" title="带看单图片">',
                    '<i class="glyphicon glyphicon-upload"></i>',
                    '</a>  '
                    ].join('');
                } else {
                    return [
                    '<a class="sawpic" href="javascript:sawPic(' + groupid + ',' + imagetype + ',' + row.runningsID + ')" title="带看单图片">',
                    '<i class="glyphicon glyphicon-picture"></i>',
                    '×' + row['itemImageType1Number'],
                    '</a>  '
                    ].join('');
                }
            },
        }, {
            field: 'itemImageType2Number',
            title: '认购单图片',
            align: 'center',
            formatter: function (value, row, index) {
                var groupid = 0;
                var imagetype = 1;
                switch ($('#ModuleID').val()) {
                    case '1':
                        groupid = 3;
                        imagetype = 2;
                        break;
                    case '2':
                    case '3':
                        groupid = 5;
                        imagetype = 1;
                        break;
                    case '4':
                        groupid = 7;
                        imagetype = 2;
                        break;
                    case '5':
                        groupid = 9;
                        imagetype = 2;
                        break;
                    default:

                }
                if (parseInt(row['itemImageType2Number']) == 0) {
                    return [
                    '<a class="sawpic" href="javascript:sawPic(' + groupid + ',' + imagetype + ',' + row.runningsID + ')" title="带看单图片">',
                    '<i class="glyphicon glyphicon-upload"></i>',
                    '</a>  '
                    ].join('');
                } else {
                    return [
                    '<a class="sawpic" href="javascript:sawPic(' + groupid + ',' + imagetype + ',' + row.runningsID + ')" title="带看单图片">',
                    '<i class="glyphicon glyphicon-picture"></i>',
                    '×' + row['itemImageType2Number'],
                    '</a>  '
                    ].join('');
                }
            },
        }, {
            field: 'itemImageType3Number',
            title: '备案图片',
            align: 'center',
            formatter: function (value, row, index) {
                var groupid = 0;
                var imagetype = 1;
                switch ($('#ModuleID').val()) {
                    case '1':
                        groupid = 3;
                        imagetype = 3;
                        break;
                    case '2':
                    case '3':
                        groupid = 5;
                        imagetype = 3;
                        break;
                    case '4':
                        groupid = 7;
                        imagetype = 3;
                        break;
                    case '5':
                        groupid = 9;
                        imagetype = 3;
                        break;
                    default:

                }
                if (parseInt(row['itemImageType3Number']) == 0) {
                    return [
                    '<a class="sawpic" href="javascript:sawPic(' + groupid + ',' + imagetype + ',' + row.runningsID + ')" title="带看单图片">',
                    '<i class="glyphicon glyphicon-upload"></i>',
                    '</a>  '
                    ].join('');
                } else {
                    return [
                    '<a class="sawpic" href="javascript:sawPic(' + groupid + ',' + imagetype + ',' + row.runningsID + ')" title="带看单图片">',
                    '<i class="glyphicon glyphicon-picture"></i>',
                    '×' + row['itemImageType3Number'],
                    '</a>  '
                    ].join('');
                }
            },
        }, {
            field: 'originStaffName',
            title: '渠道人员',
            align: 'center',
        }, {
            field: 'sellStaffName',
            title: '销售人员',
            align: 'center',
        }, {
            field: 'itemPrice',
            title: '成交价',
            align: 'center',
        }, {
            field: 'amount',
            title: '佣金',
            align: 'center',
        }, {
            field: 'settledAmount',
            title: '已结佣金',
            align: 'center',
            formatter: function (value, row, index) {

                return '<a onClick="shwosettledrunnings(' + row.id + ')" href="javascript:void(0)">' + value + '</a> ';
            },
        }, {
            field: 'tax',
            title: '税款',
            align: 'center',
            formatter: function (value, row, index) {
                var t = row.isReceipt == 1 ? '开票' : '不开款';
                return value + '&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:void(0)" onclick="modifyReceipt(' + row.id + ')">' + t + '</a>';
            },
            visible: ($('#BrokerageTypeID').val() == 3 || $('#BrokerageTypeID').val() == 7 || $('#BrokerageTypeID').val() == 10),
        }, {
            field: 'noamount',
            title: '未结佣金',
            align: 'center',
            formatter: function (value, row, index) {

                return row.amount - row.settledAmount-row.tax;
            },
        }, {
            field: 'confirmTime',
            title: '确认时间',
            align: 'center',
            visible: false,
        }, {
            field: 'confirmStaffName',
            title: '确认人',
            align: 'center',
            visible: false,
        }, {
            field: 'auditingTime',
            title: '审核时间',
            align: 'center',
            formatter: function (value, row, index) {
                if (row.state < 2) {
                    return '';
                }
                return value;
            },
            visible: false,
        }, {
            field: 'auditingStaffName',
            title: '审核人',
            align: 'center',
            visible: false,
        }, {
            field: 'state',
            title: '状态',
            align: 'center',
            formatter: function (value, row, index) {
                switch (value) {
                    case 0:
                        return '<font color="violet">●</font>待确认';
                    case 1:
                        return '<font color="blue">●</font>待审核';
                    case 2:
                        return '<font color="gleen">●</font>待结算';
                    case 3:
                        return '<font color="orange">●</font>已结算';
                    case 4:
                        return '<font color="red">●</font>已结清';
                    case 5:
                        return '<font color="gleen">●</font>待退款';
                    default:
                }

                return '';
            }
        }, {
            field: 'remark',
            title: '备注',
            align: 'left',
            formatter: function (value, row, index) {
                var temp = '';
                    temp += [
                    '<a class="remark" href="javascript:remarkEdit('+row.runningsID+')" title="修改备注">',
                    '<i class="glyphicon glyphicon-edit"></i>',
                    '</a>  '].join('');

                temp += [
                    '<label>',
                    value,
                    '</label>'
                ].join('');

                return temp;
            },
        }, {
            field: 'state',
            title: '操作',
            align: 'center',
            formatter: function (value, row, index) {
                console.log(row);
                        var a= '<a onClick="confirm(' + row.id + ')" href="javascript:void(0)">' +
                    ' <i class="fa fa-archive" aria-hidden="true"></i>佣金确认' +
                    '</a> ';
                switch (row.state) {
                    case 0:
                        switch ($('#ModuleID').val()) {
                            case '1'://新房
                                if ((isAuthority('158') && $('#BrokerageTypeID').val() == 1) || (isAuthority('158') && $('#BrokerageTypeID').val() == 2) || (isAuthority('161') && $('#BrokerageTypeID').val() == 3) || (isAuthority('161') && $('#BrokerageTypeID').val() == 4) || (isAuthority('164') && $('#BrokerageTypeID').val() == 5)) {
                                    return a;
                                }
                                break;
                            case '2'://低价房
                                if ((isAuthority('195') && $('#BrokerageTypeID').val() == 6) || (isAuthority('198') && $('#BrokerageTypeID').val() == 7) || (isAuthority('201') && $('#BrokerageTypeID').val() == 8)) {
                                    return a;
                                }
                                break;
                            case '3'://二手房
                                break;
                            case '4'://装修
                                if ((isAuthority('272') && $('#BrokerageTypeID').val() == 9) || (isAuthority('275') && $('#BrokerageTypeID').val() == 10) || (isAuthority('278') && $('#BrokerageTypeID').val() == 11)) {
                                    return a;
                                }
                                break;
                            case '5'://金融
                                if ((isAuthority('316') && $('#BrokerageTypeID').val() == 12)) {
                                    return a;
                                }
                                break;
                            default:

                        }

                        return '';
                        break;
                    case 1:
                        var a = '<a onClick="auditing(' + row.id + ')" href="javascript:void(0)">' +
                    ' <i class="fa fa-archive" aria-hidden="true"></i>佣金审核' +
                    '</a> ';
                        switch ($('#ModuleID').val()) {
                            case '1'://新房
                                if ((isAuthority('159') && $('#BrokerageTypeID').val() == 1) || (isAuthority('159') && $('#BrokerageTypeID').val() == 2) || (isAuthority('162') && $('#BrokerageTypeID').val() == 3) || (isAuthority('162') && $('#BrokerageTypeID').val() == 4) || (isAuthority('165') && $('#BrokerageTypeID').val() == 5)) {
                                    return a;
                                }
                                break;
                            case '2'://低价房
                                if ((isAuthority('196') && $('#BrokerageTypeID').val() == 6) || (isAuthority('199') && $('#BrokerageTypeID').val() == 7) || (isAuthority('202') && $('#BrokerageTypeID').val() == 8)) {
                                    return a;
                                }
                                break;
                            case '3'://二手房
                                break;
                            case '4'://装修
                                if ((isAuthority('273') && $('#BrokerageTypeID').val() == 9) || (isAuthority('276') && $('#BrokerageTypeID').val() == 10) || (isAuthority('279') && $('#BrokerageTypeID').val() == 11)) {
                                    return a;
                                }
                                break;
                            case '5'://金融
                                if ((isAuthority('317') && $('#BrokerageTypeID').val() == 12)) {
                                    return a;
                                }
                                break;
                            default:

                        }

                        return '';
                        break;
                    case 2:
                    case 3:
                        var a= '<a onClick="remittance(' + row.id + ')" href="javascript:void(0)">' +
                    ' <i class="fa fa-archive" aria-hidden="true"></i>佣金结算' +
                    '</a> ';
                        switch ($('#ModuleID').val()) {
                            case '1'://新房
                                if ((isAuthority('160') && $('#BrokerageTypeID').val() == 1) || (isAuthority('160') && $('#BrokerageTypeID').val() == 2) || (isAuthority('163') && $('#BrokerageTypeID').val() == 3) || (isAuthority('163') && $('#BrokerageTypeID').val() == 4) || (isAuthority('166') && $('#BrokerageTypeID').val() == 5)) {
                                    return a;
                                }
                                break;
                            case '2'://低价房
                                if ((isAuthority('197') && $('#BrokerageTypeID').val() == 6) || (isAuthority('200') && $('#BrokerageTypeID').val() == 7) || (isAuthority('203') && $('#BrokerageTypeID').val() == 8)) {
                                    return a;
                                }
                                break;
                            case '3'://二手房
                                break;
                            case '4'://装修
                                if ((isAuthority('274') && $('#BrokerageTypeID').val() == 9) || (isAuthority('277') && $('#BrokerageTypeID').val() == 10) || (isAuthority('280') && $('#BrokerageTypeID').val() == 11)) {
                                    return a;
                                }
                                break;
                            case '5'://金融
                                if ((isAuthority('318') && $('#BrokerageTypeID').val() == 12)) {
                                    return a;
                                }
                                break;
                            default:

                        }

                        return '';
                        break;
                    default:

                }
                return '';
            }
        }]
    });
}

function confirm(id) {
    parent.layer.open({
        type: 2,
        title: '佣金确认',
        shadeClose: false,
        shade: 0.4,
        area: ['800px', '700px'],
        content: '/Account/Brokerage/ConfirmBrokerage?ID=' + id + '&ModuleID=' + $('#ModuleID').val() + '&BrokerageTypeID=' + $('#BrokerageTypeID').val()+'&State=1',
        end: function () {
            refreshTable();
        }
    });
}


function auditing(id) {
    parent.layer.open({
        type: 2,
        title: '佣金审核',
        shadeClose: false,
        shade: 0.4,
        area: ['800px', '700px'],
        content: '/Account/Brokerage/ConfirmBrokerage?ID=' + id + '&ModuleID=' + $('#ModuleID').val() + '&BrokerageTypeID=' + $('#BrokerageTypeID').val() + '&State=2',
        end: function () {
            refreshTable();
        }
    });
}
function remittance(id) {
    parent.layer.open({
        type: 2,
        title: '佣金结算',
        shadeClose: false,
        shade: 0.4,
        area: ['500px', '350px'],
        content: '/Account/Brokerage/BrokerageSettleAccounts?ID=' + id,//?depid=' + $('#depart_id').val(),//$.getUrlParam('id'),// 
        end: function () {
            refreshTable();
        }
    });
}
function shwosettledrunnings(id) {
    parent.layer.open({
        type: 2,
        title: '佣金结算历史',
        shadeClose: false,
        shade: 0.4,
        area: ['500px', '350px'],
        content: '/Account/Brokerage/BrokerageSettleAccountsList?ID=' + id,//?depid=' + $('#depart_id').val(),//$.getUrlParam('id'),// 
        end: function () {
            refreshTable();
        }
    });
}
//备注
function remarkEdit(id) {
    var url = '';
    switch ($('#ModuleID').val()) {
        case '1'://新房
            url = '/Runnings/Runnings/RemarkEdit?id=' + id;
            break;
        case '2'://低价房
        case '3'://二手房
            url = '/LowPriceHouse/LowPriceHouseRunnings/RemarkEdit?id=' + id;
            break;
        case '4'://装修
            url = '/Decoration/DecorationRunnings/RemarkEdit?id=' + id;
            break;
        case '5'://金融
            url = '/Finance/Running/Edit?id=' + id + '&type=1';
            break;
        default:

    }
    parent.layer.open({
        type: 2,
        title: '修改备注',
        shadeClose: true,
        shade: 0.4,
        area: ['520px', '200px'],
        content: url,
        end: function () {
            refreshTable();
        }
    });
}

function sawPic(groupid,imagetype,id) {
    parent.layer.open({
        type: 2,
        title: '带看单图片',
        shadeClose: true,
        shade: 0.4,
        area: ['600px', '550px'],
        content: '/Images/Index?GroupID='+groupid+'&InterfixID=' + id + '&ImageType='+imagetype,
        end: function () {
            refreshTable();
        }
    });
}
function showIntermediaryInfo(id) {
    parent.layer.open({
        type: 2,
        title: '带看单图片',
        shadeClose: true,
        shade: 0.4,
        area: ['600px', '550px'],
        content: '/Intermediary/IntermediaryInfo?ID='+id,
        end: function () {
            refreshTable();
        }
    });
}//查询的参数
var queryParams = function (params) {
    var temp = {
        pageSize: params.limit,                             //行数
        pageIndex: (params.offset / params.limit) + 1,      //页码
        ModuleID: $('#ModuleID').val(),
        BrokerageTypeID: $('#BrokerageTypeID').val(),
        State: $('#State').val().join(),
        Search: params.search,
        BeginTime: $("#start").val(),
        EndTime:$("#end").val(),


    };
    console.log(temp);
    console.log(params);
    return temp;
};



//插入按钮
//$('#addbtn').click(function (e) {
//    parent.layer.open({
//        type: 2,
//        title: '添加金融项目',
//        shadeClose: false,
//        shade: 0.4,
//        area: ['500px', '350px'],
//        content: '/Account/Expenses/AddExpenses?ModuleID='+$('#ModuleID').val()+'&RunningsID='+$('#RunningsID').val(),//?depid=' + $('#depart_id').val(),//$.getUrlParam('id'),// 
//        end: function () {
//            refreshTable();
//        }
//    });
//});
//获取表格高度
function getHeight() {
    return $(window).height() - 150;
};
//刷新表格数据
var refreshTable = function () {
    $table.bootstrapTable('refresh');
};
function modifyReceipt(id) {
    $.ajax({
        url: "/Account/Brokerage/ModifyReceipt",
        type: "post",
        dataType: "json",
        data: {Id:id},
        success: function (data) {
            //Info,Success,Warning,Error
            var type = data.type;
            if (type == 1) {
                layer.msg(data.content, { icon: 6, time: 2000 },
                    function () {
                        refreshTable();
                    });

            } else {
                layer.msg(data.content, { icon: 5, time: 2000 });
            }
        }
    });

}
